<?php

/**
 * Role models
 *
 * Role models in bundles
 *
 * PHP Version: 1.2.3
 *
 * @author Johari <joharijumali@gmail.com>
 */
	
namespace Admin\Models\User;

use \Laravel\Database\Eloquent\Model as Eloquent;
use Laravel\Validator as Validator;
use Laravel\Auth as Auth;
use Datagrid as Datagrid;
use Laravel\Str;
use Laravel\Lang;
use Laravel\Config;

/**
 * Data Role Model Class
 *
 * Create model class for lookup data
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Role extends Eloquent
{

	public static $timestamps = true;
	public static $table = 'sys_user_role';
	public static $key = 'sur_id';
	
	/**
     * Function user
	 *
	 * @return user.
     */
	public function user()
	{
		return $this->belongs_to('Admin\Models\User\User');
	}
	
	/**
     * Function listRole
	 *
	 * @return listRole.
     */
	public static function listRole()
	{
		$rolelist = Role::paginate(Config::get('system.pagination'));

		$datagrid = new Datagrid;
		$datagrid->setFields(array(
			'sur_name'=>Str::upper(Lang::line('admin.rolename')->get()), 
			'sur_desc'=>Str::upper(Lang::line('admin.roledesc')->get())
		));
		$datagrid->setAction(Lang::line('global.edit')->get(), 'editRoleModal', true, array('sur_id'));
			//false,array('id'=>'roleid','data-toggle'=>'modal'));
		$datagrid->setAction(Lang::line('global.delete')->get(), 'deleteRole', true, array('sur_id'));
		$datagrid->setContainer('list01', 'span12');
		$datagrid->setTable('roles', 'table table-bordered table-hover table-striped table-condensed');
		$datagrid->build($rolelist, 'sur_id');

		return $datagrid->render();
	}
	
	/**
     * Function acl
	 *
	 * @return acl.
     */
	public function acl()
	{
		return $this->has_many('Admin\Models\User\Acl', 'sur_id');
	}

	/**
     * Function arrayRoles
	 *
	 * @return arrayRoles.
     */
	public static function arrayRoles()
	{
		$rolelist = Role::order_by('sur_id', 'desc')->get();

		$arrayRole = array();
		$arrayRole[0] = 'Choose Role';
		
		foreach ($rolelist as $value) {
			if (Auth::check() && Auth::user()->sur_id != 1 && $value->sur_id != 1) {
				$arrayRole[$value->sur_id] = $value->sur_name;
			} else {
				$arrayRole[$value->sur_id] = $value->sur_name;
			}
		}

		return $arrayRole;
	}

}

?>